<?php

namespace App\Modules\Apicrzs\Http\Controllers;

use App\Exceptions\Exception;
use App\Http\Controllers\Controller;
use App\Modules\Apicrzs\Http\Requests\LoginRequest;
use App\Modules\Apicrzs\Http\Requests\RegisterRequest;
use App\Modules\Apicrzs\Http\Requests\RetrieveRequest;
use App\Modules\Apicrzs\Services\LoginService;

use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Cache;


class LoginController extends BaseApicrzsController
{
    public function __construct(LoginService $authService)
    {
        $this->service = $authService;
    }
    /**
     * 登录流程
     */
    public function login(LoginRequest $request): JsonResponse
    {
        $data = $request->validated();
        $res = $this->service->login($request);
        if ($res){
            return $this->sendApiSuccess($res);
        }else{
            return $this->sendApiError($this->service->getError());
        }
    }

    /**
     * 登录流程
     */
    public function auth_login(Request $request): JsonResponse
    {
        $res = $this->service->auth_login($request);
        if ($res){
            return $this->sendApiSuccess($res);
        }else{
            return $this->sendApiError($this->service->getError());
        }
    }


    /**
     *  注册
     * @param Request $request
     * @return JsonResponse
     */
    public function register(RegisterRequest $request) : JsonResponse
    {
        $params = $request->validated();
        if ($result = $this->service->register($params)) {
            return $this->sendApiSuccess($result, $this->service->getError());
        }else{
            return $this->sendApiError($this->service->getError());
        }
    }
    /**
     *  找回密码
     * @param Request $request
     * @return JsonResponse
     */
    public function retrieve(Request $request) : JsonResponse
    {
        $params = $request->all();
        if ($result = $this->service->retrieve($params)) {
            return $this->sendApiSuccess($result, $this->service->getError());
        }else{
            return $this->sendApiError($this->service->getError());
        }
    }

    /**
     * 获取发送验证码key
     * @return mixed
     */
    public function verifyKey()
    {
        $unique = password_hash(uniqid(true), PASSWORD_BCRYPT);
        Cache::set('sms.key.' . $unique, 0, 3000);
        $time = 5;

        return $this->sendApiSuccess(['key' => $unique, 'expire_time' => $time]);
    }
    /**
     * 获取验证码
     * @return mixed
     */
    public function verifyGetCode(Request $request): JsonResponse
    {
        $params = $request->all();
        if ($result = $this->service->getVerifyCode($params)) {
            return $this->sendApiSuccess($result, $this->service->getError());
        }else{
            return $this->sendApiError($this->service->getError());
        }
    }
    /**
     * 获取图片验证码
     * @param Request $request
     */
    public function sms_captcha(Request $request){
        $params = $request->all();
        if ($result = $this->service->getVerifyCode($params)) {
            return $this->sendApiSuccess($result, $this->service->getError());
        }else{
            return $this->sendApiError($this->service->getError());
        }
    }

}
